package com.learn.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.learn.core.domain.LTheme;
import com.learn.core.mapper.LThemeMapper;
import com.learn.core.service.ILThemeService;
import com.learn.framework.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @auther CodeGenerator
 * @create 2021-03-14 13:16:28
 * @describe 服务实现类
 */
@Service
public class LThemeServiceImpl extends BaseServiceImpl<LThemeMapper, LTheme> implements ILThemeService {

    @Autowired
    LThemeMapper lThemeMapper;
    @Override
    public List<LTheme> queryLThemeAll() {
        List<LTheme> treeMenus =new ArrayList<>();
        QueryWrapper<LTheme> queryWrapper = new QueryWrapper<LTheme>();
        queryWrapper.eq("theme_state","1");
        List<LTheme> lThemeList = lThemeMapper.selectList(queryWrapper);
        // 查询树结构
        for(LTheme lTheme : getRootNode(lThemeList)) {
            lTheme=buildChilTree(lTheme,lThemeList);
            treeMenus.add(lTheme);
        }
        return treeMenus;
    }

    //递归，建立子树形结构
    private LTheme buildChilTree(LTheme pNode,List<LTheme> menuList){
        List<LTheme> chilMenus =new ArrayList<>();
        for(LTheme menuNode : menuList) {
            if(menuNode.getParentNode() == pNode.getThemeId()) {
                chilMenus.add(buildChilTree(menuNode,menuList));
            }
        }
        pNode.setChilLTheme(chilMenus);
        return pNode;
    }

    //获取根节点
    private List<LTheme> getRootNode(List<LTheme> menuList) {
        List<LTheme> rootMenuLists =new  ArrayList<LTheme>();
        for(LTheme menuNode : menuList) {
            if(menuNode.getParentNode() == 0) {
                rootMenuLists.add(menuNode);
            }
        }
        return rootMenuLists;
    }
}
